package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.luck.easyfoodbatch.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/22
 */
public class FindFragment extends BaseFragment {
    private LinkedHashMap<String, Fragment> mFragmentsList;
    private FindFragment01 mFindFragment01;
    private FindFragment02 mFindFragment02;
    private FindFragment03 mFindFragment03;
    private View rootView;
    private ViewPager vpShow;
    private TabLayout mTabLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_find, container, false);
            View mStateBarFixer = rootView.findViewById(R.id.status_bar_fix);
            mStateBarFixer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(getActivity())));
            mStateBarFixer.setBackgroundColor(getResources().getColor(R.color.black));
            initViewPager(rootView);
        }
        return rootView;
    }

    private void initViewPager(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.tb_find_item);
        mTabLayout.setTabTextColors(getResources().getColor(R.color.black), getResources().getColor(R.color.tab_color_true));
        vpShow = (ViewPager) view.findViewById(R.id.vp_list_item_show);
        mFindFragment01 = new FindFragment01();
        mFindFragment02 = new FindFragment02();
        mFindFragment03 = new FindFragment03();
        vpShow.setOffscreenPageLimit(0);
        vpShow.setAdapter(new MyPageAdapter(getFragmentManager()));
        mTabLayout.setupWithViewPager(vpShow);
    }

    class MyPageAdapter extends FragmentPagerAdapter {
        private ArrayList<String> titles;

        public MyPageAdapter(FragmentManager fm) {
            super(fm);
            mFragmentsList = new LinkedHashMap<>();
            mFragmentsList.put("精选", mFindFragment01);
            mFragmentsList.put("资讯", mFindFragment02);
            mFragmentsList.put("推荐", mFindFragment03);
            titles = new ArrayList<>();
            titles.addAll(mFragmentsList.keySet());
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentsList.get(titles.get(position));
        }

        @Override
        public int getCount() {
            return mFragmentsList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
